/**
 * Created by Administrator on 2017/6/28 0028.
 */
// add group
$('#pages-groups-addgroup-btn').delegate(".btn-primary", "click", function () {
    var groupname = $('#pages-groups-groupname-input input').val().trim();
    var groupdes = $('#pages-groups-groupdesc-input textarea').val();
    if (groupname.length == 0) {
        alert('组名不能为空')
    } else {
        $.ajax({
            url: '/users/groupservice/',
            type: 'POST',
            data: {'groupname': groupname, 'groupdes': groupdes, 'flag': 'add'},
            success: function (data) {
                var obj = JSON.parse(data);
                if (obj.msg == true) {
                    var td1 = "<td>" + groupname + "</td>";
                    var td2 = "<td>" + obj.usernum + "</td>";
                    var td3 = "<td>" + groupdes + "</td>";
                    var td4 = '<td><button type="button" class="btn btn-info pages-groups-edit-group" data-toggle="modal" data-target="#pages-groups-editgroup-Modal">编辑</button>&nbsp; &nbsp; <button type="button" class="btn btn-danger pages-groups-del-group">删除</button> </td>';
                    var trtag = document.createElement('tr');
                    $(trtag).append(td1);
                    $(trtag).append(td2);
                    $(trtag).append(td3);
                    $(trtag).append(td4);
                    $(trtag).attr('flag', obj.gid);
                    $('#pages-groups-table-list .table-striped tbody').append(trtag);

                }
            }

        });
    }
    $('#pages-groups-addgroup-Modal').modal('hide')
});

//del group
$('#pages-groups-table-list').delegate(".pages-groups-del-group", "click", function () {
    var scope = this;
    var groupid = $(scope).parent().parent().attr('flag');

    $.ajax({
        url: '/users/groupservice/',
        type: 'POST',
        data: {'id': groupid, 'flag': 'del'},
        success: function (data) {
            if (data == 'ok') {
                $(scope).parent().parent().remove();
            }
        }
    })
});


// 编辑时获取数据
$('#pages-groups-table-list').delegate(".pages-groups-edit-group", "click", function () {
    var self = this;
    var obj = $(self).parent().parent().children();
    var groupname = obj.eq(0).text();
    var groupdes = obj.eq(2).text();
    $('#pages-groups-edit-groupname').val(groupname);
    $('#pages-groups-edit-groupdesc').val(groupdes);
    // var flag=$(this).parent().parent().attr("flag");
    $(self).addClass('activedit')

});
//提交时获取
$('.pages-groups-edit-submit .btn-primary').click(function () {
    var newGroupname = $('#pages-groups-edit-groupname').val().trim();
    var newGroupdes = $('#pages-groups-edit-groupdesc').val();
    var obj = $('#pages-groups-table-list').find('.activedit');
    var flag = obj.parent().parent().attr('flag');
    if (newGroupname.length = 0) {
        alert('组名不能为空')
    } else {
        $.ajax({
            url: '/users/groupservice/',
            data: {'groupname': newGroupname, 'groupdes': newGroupdes, 'flag': 'edit', "id": flag},
            type: 'POST',
            success: function (data) {
                if (data == 'ok') {
                    obj.eq(0).text(newGroupname);
                    obj.eq(2).text(newGroupdes);
                }
            }
        })
    }
    $('#pages-groups-editgroup-Modal').modal('hide');
});

// 当模态框隐藏式，初始化
$('#pages-groups-addgroup-Modal').on('hidden.bs.modal', function () {
    $('#pages-groups-groupname-input input').val('');
    $('#pages-groups-groupdesc-input textarea').val('');
});
